package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.CheckItemDeleteFailException;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

            String queryString = queryPageBean.getQueryString();
            Integer currentPage = queryPageBean.getCurrentPage();
            Integer pageSize = queryPageBean.getPageSize();
            PageHelper.startPage(currentPage,pageSize);
            List<Permission> permissionList;
            if(queryString != null){
                permissionList = permissionDao.getByQueryString(queryString);
            }else{
                permissionList = permissionDao.getAllPermission();
            }
            PageInfo pageInfo = new PageInfo<>(permissionList);
            return new PageResult(pageInfo.getTotal(),permissionList);

    }

    @Override
    public Result add(Permission permission) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(new Date());
            permission.setCreationDate(format);
            permissionDao.insertPermission(permission);
            return new Result(false,"新增权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增权限失败");
        }
    }

    @Override
    public Result findById(Integer permissionId) {
        try {
            Permission permission = permissionDao.findById(permissionId);
            return new Result(true,"查询权限详细信息成功",permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,"查询权限详细信息失败");
        }
    }

    @Override
    public Result updatePermission(Permission permission) {
        try {
            permissionDao.updatePermission(permission);
            return new Result(true,"修改权限信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改权限信息失败");
        }
    }

    @Override
    public Result deletePermission(Integer permissionId) {
        try {
            Integer count = permissionDao.getByPermissionId(permissionId);
            if(count > 0){
                throw new CheckItemDeleteFailException();
            }
            permissionDao.deletePermission(permissionId);

            return new Result(true,"删除权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            if(e instanceof CheckItemDeleteFailException){
                return new Result(false, "删除权限失败，是因为有角色正在关联该菜单");
            }
            return new Result(false,"删除失败，请稍后再试");
        }
    }

}
